#!/usr/bin/env python
#-*- coding:utf-8 -*-

import gtk
import gobject


from ByteManip import *
from VisualByte import *
from VisualBit import *

    
def sea54(x, bit4, show_info = True):

    # tenta em ordem, do que menos altera ao que mais altera      
    fatores = [ 0, -1, +1, -2, +2, -3, +3, -4, +4 ]
    x_bits = Byte(x)
    
    def num(x):
        return ( "%3d" % x ) + " (" + Byte(x).to_str() + ")"
    
    st = num(x) + " -> "
        
    for fator in fatores:
        
        valor = x + fator
        if valor < 0 or valor > 255:
            continue
        
        valor_bits = Byte(valor)
        
        diff = abs( x - valor )
        
        if valor_bits.get_bit(4) == bit4:
            if show_info:
                print( st + num(valor) + " OK! - Diferença: " + str(diff) )
            return valor
    
    if show_info:    
        print( st + "  NÃO ENCONTROU!" )
    return -1
    


class Form:    
    def __init__(self):
        b = gtk.Builder()
        b.add_from_file( "sea54.glade" )
        
        self.window = b.get_object( "window" )
        self.adjustmentX = b.get_object( "adjustmentX" )
        self.labResp = b.get_object( "labResp" )
        self.radBin1 = b.get_object( "radBin1" )
        self.radBin2 = b.get_object( "radBin2" )
        self.labPossivel = b.get_object( "labPossivel" )
        self.labDiff = b.get_object( "labDiff" )
        self.areaX = b.get_object( "areaX" )
        self.areaResp = b.get_object( "areaResp" )
        self.btnGerarTabela = b.get_object( "btnGerarTabela" )
        
        self.byteX = VisualByte()
        self.byteX.bit4.set_special_color()
        self.areaX.add( self.byteX )        
        
        self.byteResp = VisualByte()
        self.byteResp.bit4.set_special_color()
        self.areaResp.add( self.byteResp )
    
    def get_x(self):
        return int( self.adjustmentX.get_value() )



def main():
    
    def fechou(*args):
        gtk.main_quit()
    
    def mudou_val(sender, form):
        form.byteX.set_decimal( form.get_x() )
        roda(form)
    
    def mudou_bin(sender, form):
        roda(form)
    
    def roda(form):
        bit4 = 1 if form.radBin1.get_active() else 0        
        resp = sea54( form.get_x(), bit4 )
        
        if resp == -1:
            form.labPossivel.set_text( "Não" )
            form.labDiff.set_text("-")
            form.byteResp.risca()
        else:
            form.labResp.set_text( str(resp) )
            form.byteResp.set_decimal( resp )
            
            diff = abs( form.get_x() - resp )
            
            form.labPossivel.set_text( "Sim" )
            form.labDiff.set_text( str(diff) )
            form.byteResp.un_risca()
    
    def gerar_tabela(*args):
        print( "# Para ativar o bit 4" )
        print( "sea54_mask_on = {}" )
        
        for i in range(256):
            resp = sea54( i, 1, show_info = False )
            print( "sea54_mask_on[%d] = %d" % (i, resp) )
                
        print
        print

        print( "# Para desativar o bit 4" )
        print( "sea54_mask_off = {}" )
        
        for i in range(256):
            resp = sea54( i, 0, show_info = False )
            print( "sea54_mask_off[%d] = %d" % (i, resp) )
        
        print( "# - Fim da tabela -" )
                
               
    form = Form()        
    form.adjustmentX.connect( "value-changed", mudou_val, form )
    form.radBin1.connect( "toggled", mudou_bin, form )
    form.radBin2.connect( "toggled", mudou_bin, form )
    form.window.connect( "delete-event", fechou )
    form.btnGerarTabela.connect( "clicked", gerar_tabela )    
    
    form.window.show()
    gtk.main()
    


main()
